package io.zerous.zexgraph;

import android.content.Context;
import android.util.Log;

/**
 * Created by Zerous on 2018/7/22.
 */
public class LuaMachine {
    private final String LUA_ERROR_TAG = "LUA-ERROR";

    static {
        System.loadLibrary("native-lib");
    }

    private String currentSrc = "";
    private boolean haveLoaded = false;

    public LuaMachine() {
        initLuaInterpreter();
    }

    public void loadScript(String src) {
        currentSrc = src;
        haveLoaded = luaLoadScript(currentSrc);
    }

    public float runScript(float x) {
        if (haveLoaded) {
            return luaRun(x);
        }
        Log.e(LUA_ERROR_TAG, "Can not run because there are no script exists.");
        return 0;
    }

    private native void initLuaInterpreter();

    private native boolean luaLoadScript(String src);

    private native float luaRun(float x);

    private native float getOutputf(String name);

    private native void setOutputf(float n);
}
